﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UI.WebPage.Background;
using Model.Cms;
using Model.Config;
using BLL.Cms;
using Com.Ompchina.Lib.Draw;
namespace Com.Ompchina.Web.admin.cms
{
    public partial class add_node : AddInfoPage, IAddInfo
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                base.SetCKFinder("node", ckeContent);
                if (PageAction.Equals("update") || PageAction.Equals("clone"))
                    this.InitSingleInfo(base.GetUpdateID());
            }
        }

        #region IAddInfo 成员

        public bool CheckInput()
        {
            if (ddlTerm.SelectedIndex == 0)
            {
                this.SetMessage(false, "请选择分类");
                return false;
            }
            else if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                this.SetMessage(false, "请输入页面名称");
                return false;
            }
            else if (string.IsNullOrEmpty(ckeContent.Text.Trim()))
            {
                this.SetMessage(false, "请输入页面内容");
                return false;
            }
            return true;
        }

        public void InitSingleInfo(int id)
        {
            if (id > 0)
            {
                NodeInfo info = new NodeBLL().GetNodeByID(id);
                if (info.NodeID > 0)
                {
                    ddlTerm.DefaultSelect = info.NodeChannel.TermID.ToString();
                    txtName.Text = info.NodeName;
                    txtSeo.Text = info.SeoKeyword;
                    txtHeader.Text = Server.HtmlDecode(info.HeadScript);
                    ckeContent.Text = Server.HtmlDecode(info.BodyContent);                    
                    if (PageAction.Equals("update"))
                        hfID.Value = id.ToString();
                }
            }
        }

        #endregion

        #region 页面私有方法
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                NodeBLL bll = new NodeBLL();
                if (hfID.Value.Equals("0"))
                {
                    if (bll.InsertNode(int.Parse(ddlTerm.SelectedValue),HtmlEncode(txtName.Text),HtmlEncode(txtSeo.Text),HtmlEncode(txtHeader.Text)
                        , HtmlEncode(ckeContent.Text)) > 0)
                    {
                        this.SetMessage(true, "创建节点成功");                        
                        base.Redirect("list_node.aspx?action=list" + base.GetQueryStringAdd(), 1);
                    }
                    else
                        this.SetMessage(false, "创建节点失败，请稍后重试");
                }
                else
                {                
                    if (bll.UpdateNode(
                        int.Parse(hfID.Value),int.Parse(ddlTerm.SelectedValue),HtmlEncode(txtName.Text),HtmlEncode(txtSeo.Text)
                        ,HtmlEncode(txtHeader.Text) , HtmlEncode(ckeContent.Text)
                        ))
                    {
                        this.SetMessage(true, "修改节点成功");                        
                        base.Redirect("list_node.aspx?action=list" + base.GetQueryStringAdd(), 1);
                    }
                    else
                        this.SetMessage(false, "修改节点失败，请稍后重试");
                }
            }
        }
        #endregion        
    }
}
